package com.atguigu.dga.assessor.spec;

import com.atguigu.dga.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component("TABLE_NAME_STANDARD")
public class TableNameStandardAssessor extends Assessor {
    //正则匹配规则
    Pattern odscompile = Pattern.compile("^(ods)_\\w+_(inc|full)$");
    Pattern dimcompile = Pattern.compile("^(dim)_\\w+(_(zip|full)$)?");
    Pattern dwdcompile = Pattern.compile("^(dwd)_\\w+_\\w+_(inc|full|acc)$");
    Pattern dwscompile = Pattern.compile("^(dws)_\\w+_\\w+_\\w+_(1d|nd|td)$");
    Pattern adscompile = Pattern.compile("^(ads)_\\w+$");
    Pattern dmcompile = Pattern.compile("^(dm)_\\w+$");
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws ParseException {
        //获取表名
        String tableName = assessParam.getTableMetaInfo().getTableName();
        //获取分层
        String dwLevel = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getDwLevel();

        Matcher matcher = null;
        //判断所在层级
        if ("ODS".equals(dwLevel)){
             matcher = odscompile.matcher(tableName);
        }else if ("DIM".equals(dwLevel)){
             matcher = dimcompile.matcher(tableName);
        }else if ("DWD".equals(dwLevel)){
             matcher = dwdcompile.matcher(tableName);
        }else if ("DWS".equals(dwLevel)){
             matcher = dwscompile.matcher(tableName);
        }else if ("ADS".equals(dwLevel)){
             matcher = adscompile.matcher(tableName);
        }else if ("DM".equals(dwLevel)){
             matcher = dmcompile.matcher(tableName);
        }else {
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(5));
            governanceAssessDetail.setAssessProblem("未纳入分层");
        }
        if (!matcher.matches()){
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(0));
            governanceAssessDetail.setAssessProblem("表名不符合规范");
            //获取通用路径
            String governanceUrl = assessParam.getGovernanceMetric().getGovernanceUrl();
            //路径替换
            governanceUrl=governanceUrl.replace("{tableId}",assessParam.getTableMetaInfo().getId() + "");
            governanceAssessDetail.setGovernanceUrl(governanceUrl);
        }


    }
}
